<template>
  <div class="expenses-claim">
    <div style="margin-bottom: 10px">
      <el-button size="small" type="primary" @click="add">经费报销</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="tableData"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="标题" prop="title" show-overflow-tooltip />
      <el-table-column
        label="费用类型"
        align="center"
        prop="amountTypeName"
        width="120"
      />
      <el-table-column
        label="预算金额"
        align="center"
        prop="amountBudget"
        width="100"
      />
      <el-table-column
        label="执行金额"
        align="center"
        prop="amountExecute"
        width="100"
      />
      <el-table-column label="发生日期" align="center" prop="occurTime" />
      <el-table-column label="付款日期" align="center" prop="payTime" />
      <el-table-column label="状态" align="center" width="120">
        <template slot-scope="{ row }">
          {{ renderStatus(row.status) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="90">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.status == null"
            size="mini"
            type="danger"
            plain
            @click="revoke(row)"
          >
            撤销
          </el-button>
          <el-button
            v-else-if="row.status === 0"
            size="mini"
            type="warning"
            plain
            @click="print(row)"
          >
            打印
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top: 10px" />
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100]"
      :current-page="pageNum"
      :page-size="pageSize"
      :total="total"
      @size-change="onPageSizeChange"
      @current-change="onCurrentPageChange"
    />

    <el-dialog
      width="650px"
      title="经费报销"
      :visible.sync="visible"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="6em">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="费用类型" prop="amountType">
              <el-select
                style="width: 100%"
                v-model="form.amountType"
                placeholder="请选择费用类型"
              >
                <el-option
                  v-for="item in amountTypeList"
                  :key="item.fundsEnumKey"
                  :value="item.fundsEnumKey"
                  :label="item.fundsEnumName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预算金额" prop="amountBudget">
              <el-input-number
                style="width: 100%"
                v-model="form.amountBudget"
                :min="0"
                placeholder="请输入预算金额"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行金额" prop="amountExecute">
              <el-input-number
                style="width: 100%"
                v-model="form.amountExecute"
                :min="0"
                placeholder="请输入执行金额"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发生日期" prop="occurTime">
              <el-date-picker
                style="width: 100%"
                v-model="form.occurTime"
                placeholder="请选择发生日期"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款日期" prop="payTime">
              <el-date-picker
                style="width: 100%"
                v-model="form.payTime"
                placeholder="请选择付款日期"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="附件" prop="fileList">
          <el-upload
            :file-list="form.fileList"
            :headers="upload.headers"
            :action="upload.action"
            :before-upload="beforeUpload"
            :on-success="handleSuccess"
            :on-remove="handleRemove"
          >
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>

        <el-form-item class="dialog-footer">
          <el-button @click="visible = false">关 闭</el-button>
          <el-button type="primary" @click="submit">提 交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import printJS from "print-js";
import {
  reimbursementPage,
  getSubjectList,
  saveReimbursement,
  cancelReimbursement,
  printReimbursement,
} from "@/api2/projectManagement/process";
import { getToken } from "@/utils/auth";
import { uploadUrl } from "@/const";
// const base_url = process.env.VUE_APP_BASE_APITwo;

export default {
  inject: ["projectId"],
  data() {
    return {
      tableData: [],
      listLoading: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      visible: false,
      form: {},
      rules: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        amountType: [
          { required: true, message: "费用类型不能为空", trigger: "change" },
        ],
        fileList: [
          { required: true, message: "附件不能为空", trigger: "change" },
        ],
      },
      amountTypeList: [],
      upload: {
        headers: { Authorization: getToken(), platform: 'WEB' },
        action: uploadUrl,
        // action: base_url + "/web/dj/cost/uploadDjCostExcel",
      },
    };
  },
  mounted() {
    this.getOption();
    this.getList();
  },
  methods: {
    renderStatus(status) {
      const obj = {
        0: "已通过",
        1: "已驳回",
        2: "已撤销",
      };
      return obj[status] || "待审核";
    },
    async getOption() {
      const { data } = await getSubjectList({ projectId: this.projectId });
      this.amountTypeList = data;
    },
    getList() {
      this.listLoading = true;
      reimbursementPage({
        projectId: this.projectId,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }).then((data) => {
        this.listLoading = false;
        this.tableData = data.data;
        this.total = data.total;
      });
    },
    onPageSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getList();
    },
    onCurrentPageChange(val) {
      this.pageNum = val;
      this.getList();
    },
    add() {
      this.visible = true;
      this.resetForm();
      this.form = { fileList: [] };
    },
    revoke({ reimburseId }) {
      this.$confirm("是否要撤销审批?", {
        type: "warning",
      })
        .then(async () => {
          await cancelReimbursement({ reimburseId });
          this.$message.success("撤销成功");
          this.getList();
        })
        .catch(() => {});
    },
    async print({ reimburseId }) {
      const res = await printReimbursement({ id: reimburseId });
      printJS(res.data.url);
    },
    beforeUpload(rawFile) {
      const isLt10M = rawFile.size / 1024 / 1024 < 10;
      if (!isLt10M) {
        this.$message.error("文件大小限制在 10MB 内!");
        return false;
      }
    },
    handleSuccess(res, file) {
      if (res.code === 0) {
        const { fileName: name, url } = res.data;
        this.form.fileList.push({ name, url });
      } else {
        this.form.fileList?.splice(this.form.fileList.indexOf(file), 1);
      }
    },
    handleRemove(file, fileList) {
      this.form.fileList?.splice(this.form.fileList.indexOf(file), 1);
    },
    submit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const {
            reimburseId,
            title,
            amountType,
            amountBudget,
            amountExecute,
            occurTime,
            payTime,
            fileList,
          } = this.form;
          const params = {
            projectId: this.projectId,
            title,
            amountType,
            amountBudget,
            amountExecute,
            occurTime,
            payTime,
            fileList,
          };
          reimburseId && (params.reimburseId = reimburseId);
          const tips = reimburseId ? "修改成功" : "新增成功";
          await saveReimbursement(params);
          this.$message.success(tips);
          this.visible = false;
          this.getList();
        }
      });
    },
    resetForm() {
      if (!this.$refs.form) return;
      this.$refs.form.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.dialog-footer {
  margin-bottom: 0;
  text-align: center;
  ::v-deep {
    .el-form-item__content {
      margin-left: 0 !important;
    }
  }
}
</style>
